/* eslint-disable no-else-return */
window.customRunTest = function runTest1(testUrl, installServer, mimeType) {
	var minVersion = 1;
	var installName = '';
	var confirmMessage = '';

	// Get optional variables    
	if (arguments.length > 3) { minVersion = arguments[3]; }
	if (arguments.length > 4)
		installName = arguments[4];
	if (arguments.length > 5)
		confirmMessage = arguments[5];

	if (!isDriverLoaded(mimeType, minVersion)) {
		customInstallDriver(installServer, mimeType, installName, confirmMessage);
	}
	else {
		window.appex.openEts({ url: testUrl });
		console.log(testUrl);
	}


	return false;
}

window.customInstallDriver = function installDriver(installServer, mimeType) {
	var installName = '';
	var confirmMessage = '';

	// Get optional variables    
	if (arguments.length > 2)
		installName = arguments[2];
	if (arguments.length > 3)
		confirmMessage = arguments[3];

	if (installName == null || installName.length == 0)
		installName = 'secure browser';

	if (confirmMessage == null || confirmMessage.length == 0)
		confirmMessage = 'The ' + installName + ' is currently not installed on your system\n or does not meet the minimum version required.\r\n\r\nClick OK to install the ' + installName + ' now, or click cancel.';

	if (confirm(confirmMessage)) {
		if (installServer.substring((installServer.length - 1), 1) == '/')
			window.appex.openExternal((installServer + 'Drivers/InstallDriver.aspx?mime=' + mimeType), false);
		else
			window.appex.openExternal((installServer + '/Drivers/InstallDriver.aspx?mime=' + mimeType), false);
	}
}

window.customContinueTest = function (vLink, vTestName, vITDVersion) {

	var bAgent = window.navigator.userAgent;

	if (vTestName && vTestLaunched[vTestName]) return;

	var vRes = window.screen.width + 'x' + window.screen.height;
	var vColors = window.screen.colorDepth;
	vLink = vLink + '&res=' + vRes + '&color=' + vColors;

	if (vLink.indexOf('&resultid=') == -1) { //launch new test
		if (vTestName.indexOf('Speaking') == -1 && vTestName.indexOf('Next Generation') != -1) { //load customize test page for next gen non-speaking test
			startTest(vLink, vTestName, vITDVersion);
			return;
		}
		if (vTestName.indexOf('iBT') != -1 && vTestName.indexOf('Free') != -1) {//load customize test page for ibt full free test
			startTest(vLink, vTestName, vITDVersion);
			return;
		}
		if (vTestName.indexOf('TOEFL iBT Reading, Listening, Writing Practice Test') != -1) {//load customize test page for ibt full free test
			startTest(vLink, vTestName, vITDVersion);
			return;
		}
		if (vTestName.indexOf('TOEFL iBT Complete Practice Test') != -1) {//load customize test page for ibt full free test
			startTest(vLink, vTestName, vITDVersion);
			return;
		}
		if (vTestName.indexOf('Complete V') != -1) {//load customize test page
			startTest(vLink, vTestName, vITDVersion);
			return;
		}
		if (vTestName.indexOf('iBT Speaking and Writing Practice Test') != -1) {//load customize test page
			startTest(vLink, vTestName, vITDVersion);
			return;
		}
		if (vTestName.indexOf('iBT Reading and Listening Practice Test') != -1) {//load customize test page
			startTest(vLink, vTestName, vITDVersion);
			return;
		}

		if (vTestName.indexOf('New Oriental Reading and Listening Practice Test') != -1) {//load customize test page
			startTest(vLink, vTestName, vITDVersion);
			return;
		}

		if (vTestName.indexOf('New Oriental Speaking and Writing Practice Test') != -1) {//load customize test page
			startTest(vLink, vTestName, vITDVersion);
			return;
		}
		if (vTestName.indexOf('Item') != -1 || vTestName.indexOf('Next Generation') == -1) vLink += '&OR=4'; //disable notes for legacy tests
	}

	//our boss wants all tests for toefl launch in http mode

	var my_chk = ExecuteCall("checkmacsb.aspx?test=" + vTestName);

	if (my_chk == "true") {
		window.location = "macdrivers/macdownload.htm";
		return;
	} else if (my_chk == "na") {
		window.location = "macdrivers/macNA.htm";
		return;
	} else if (my_chk == "ok") {
		launchTestMac(vLink);
		return;
	}

	if (isSB()) {

		if (sb_test_launched) return;
		var vNewUrl = window.location.host + window.location.pathname
		vNewUrl = vNewUrl.replace('starttest.aspx', '');
		vNewUrl = vNewUrl.replace('systemcheck.aspx', '');
		vLink = vLink.replace('DR=', 'ddddddd=');

		sb_test_launched = true;

		//if(IsHybridAvailable(vITDVersion)) {
		if (IsHybridAvailable('5.3.0.0')) {
			window.location = 'https://' + vNewUrl + vLink + '&hybrid=1';
			return;
		} else {
			window.location = 'https://' + vNewUrl + vLink;
			return;
		}
	} else {
		//var pos = vLink.indexOf("DR=");
		if (true) {
			//var DR = parseInt(vLink.substr(pos+3));
			if (true) {
				if (sb_test_launched) return;
				sb_test_launched = true;

				var vNewUrl = window.location.host + window.location.pathname
				vNewUrl = vNewUrl.replace('starttest.aspx', '');
				vNewUrl = vNewUrl.replace('systemcheck.aspx', '');
				vLink = 'https://' + vNewUrl + vLink;

				if (arguments.length > 1) vLink += '&dupchk=true';

				//window.location = vLink + '&gco=1';
				window.appex.openEts({ url: vLink + '&gco=1' });
				console.log(vLink);
				window.setTimeout(loadHomePage, 30000);
				//window.setTimeout(function () { if (gco_window != null) gco_window.close() }, 5000);

				return;
			}
		}
	}

	if (true) { //force test to launch in http mode
		var vNewUrl = window.location.host + window.location.pathname
		vNewUrl = vNewUrl.replace('starttest.aspx', '');
		vNewUrl = vNewUrl.replace('systemcheck.aspx', '');
		vLink = 'https://' + vNewUrl + vLink;
	}

	if (window.navigator.appName.indexOf('Netscape') != -1 && window.navigator.userAgent.indexOf('Mac') != -1)
		itd = window.open(vLink, 'launchtest', 'toolbar=no,menubar=no,location=no,directories=no,screenX=0,screenY=0,width=' + (window.screen.availWidth - 20) + ',height=' + (window.screen.availHeight - 40) + ',fullscreen=yes');
	else if (window.navigator.appName.indexOf('Netscape') != -1)
		itd = window.open(vLink, 'launchtest', 'toolbar=no,menubar=no,location=no,directories=no,screenX=0,screenY=0,width=' + window.screen.availWidth + ',height=' + (window.screen.availHeight - 20) + ',fullscreen=yes');
	else if (window.navigator.userAgent.indexOf('Mac') != -1) {
		itd = window.open(vLink, 'launchtest', 'resizeable=yes');
		itd.resizeTo(window.screen.width, window.screen.height);
		itd.moveTo(0, 0);
	} else
		itd = window.open(vLink, 'launchtest', 'toolbar=no,menubar=no,location=no,directories=no,fullscreen=yes');

	if (itd == null) {
		alert("Your browser has pop-up blocker enabled, this can prevent the test window from opening properly. If your test did not launch, disable your pop-up blocker and try again.");
		return;
	}

	if (vTestName) vTestLaunched[vTestName] = true;
	intervalID = window.setInterval('checkTestWindow()', 200);
}

window.addEventListener("load", function (event) {
	const url = new URL(location.href);
	if (url.hostname !== 'toeflpractice.ets.org' ||
		!(url.pathname.startsWith('/toeflgateway.aspx') || url.pathname.startsWith('/starttest.aspx'))) {
		return;
	}
	eval('class ActiveXObject{get Installed(){return true;}get Version(){return 15;}constructor(id){}}; window.ActiveXObject = ActiveXObject;');
	window.runTest = customRunTest;
	window.continueTest = customContinueTest;
});